package net.sansi.v3correctserver.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sansi.v3correctserver.constenum.AppHelper;
import net.sansi.v3correctserver.dto.R;
import net.sansi.v3correctserver.dto.camera.CameraDto;
import net.sansi.v3correctserver.service.camera.Camera;
import net.sansi.v3correctserver.service.camera.CameraService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Fang
 * create time : 2025/1/22
 * IDE : IntelliJ IDEA
 */

@Api(tags = "相机控制接口")
@Slf4j
@RestController
@RequestMapping("/camera")
public class CameraController {

    private final CameraService cameraService;

    public CameraController(CameraService cameraService) {this.cameraService = cameraService;}

    @ApiOperation("查询相机列表")
    @GetMapping("/query/list")
    public R<List<Camera>> list() {
        return R.data(Camera.cameras());
    }

    @ApiOperation("连接相机")
    @PostMapping("/connect")
    public R<CameraDto.ConnectResp> connect(@RequestParam("cameraName") String cameraName) {
        cameraService.connectCamera(cameraName);
        return R.data(new CameraDto.ConnectResp()
                .setCameraParams(AppHelper.projectSpace().getCorrectCnf().getCameraParams())
                .setOptions(Camera.find(cameraName).readOptions(false))
        );
    }

    @ApiOperation("断开相机连接")
    @PostMapping("/disconnect")
    public R<String> disconnect(@RequestParam("cameraName") String cameraName) {
        return R.status(
                Camera.find(cameraName).disconnect()
        );
    }

}
